package sucre

class LapsoAcademicoController {
    def loginService
    def beforeInterceptor = [action:this.&auth]
    def auth = {
        def result =loginService.validarAcceso(
            session["seguridad_usuario"],
            session["seguridad_funcionesHabilitadas"],
            controllerName,actionName,request.getRemoteAddr().toString())
        if(!result) {
            redirect(controller:"login",action:"auth",params:['acceso':false])
            return false
        }
    }
    def index = {}

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def parametersService
    def list = {
        params.max = parametersService.getInt("paginacion.maxRegPagina")
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        def query={
            if(params.searchField){
                eq('nombre',params.searchField)
            }
        }
        def lapsoList=LapsoAcademico.createCriteria().list(params,query)
        [lapsoInstanceList:  lapsoList,
         lapsoInstanceTotal: lapsoList.getTotalCount()]
    }

    def create = {
        def lapsoInstance = new LapsoAcademico()
        lapsoInstance.properties = params
        if(params.eOc=='edit'){
            lapsoInstance=LapsoAcademico.findById(params.id)
        }else{
            flash.message=''
            params.eOc='create'
        }
        return [lapsoInstance:lapsoInstance,eOc:params.eOc]
    }
    def save = {
        def listaErrores=[]
        def lapsoInstance = new LapsoAcademico(params)
        if(params.eOc=='edit'){
            println "params.id: " + params.id
            
            lapsoInstance=LapsoAcademico.get(params.id)
            println  "LapsoAcademico: " + lapsoInstance
            lapsoInstance.properties=params
        }
        
        LapsoAcademico.withTransaction{ status ->
            try{
                if(!lapsoInstance.hasErrors() && lapsoInstance.save(flush:true)) {

                    if(params.eOc=='create'){
                        //registrar auditoria def.titular.creado
                        flash.message=message(code:'def.registro.creado',
                            args:[lapsoInstance])
                    }else{
                        //def.titular.actualizado
                        flash.message=message(code:'def.registro.actualizado',
                            args:[lapsoInstance])
                    }
                        
                    if(params.eOc=='create'){
                        params.eOc='edit'
                    }
                }
            }catch(Exception e){
                println e
                listaErrores.add(message(code:'def.registro.error.Creando'))
                status.setRollbackOnly()
            }
        }
        render(view:'create',model:[lapsoInstance:lapsoInstance,
                eOc:params.eOc,upd:'si',listaErrores:listaErrores])
    }
    def search={
        redirect(action:list,params:[searchField:params.searchField])
    }
    def delete = {
        def listaErrores=[]
        def lapsoInstance = LapsoAcademico.get(params.id)
        LapsoAcademico.withTransaction{ status ->
        lapsoInstance.delete(flush:true)
        flash.message=message(code:'def.noeliminado', ags:[lapsoInstance])
        render(view:'create',model:[lapsoInstance:new LapsoAcademico(),upd:'si',
                    listaErrores:listaErrores])
        }
    }
}
